﻿namespace LeetCodeProject._0001_1000._001_100._51_60._059;

public class Solution
{
    public int[][] GenerateMatrix(int n)
    {
        var step = new (int row, int col)[] { (0, 1), (1, 0), (0, -1), (-1, 0) };

        var k = 0;
        var (row, col) = (0, 0);
        var matrix = new int[n][];
        for (int i = 0; i < n; i++)
        {
            matrix[i] = new int[n];
        }

        for (int i = 1; i <= n * n; i++)
        {
            matrix[row][col] = i;
            var r1 = row + step[k].row;
            var c1 = col + step[k].col;
            if (r1 < 0 || r1 >= n || c1 >= n || c1 < 0 || matrix[r1][c1] != 0)
            {
                k = (k + 1) % step.Length;
            }

            row += step[k].row;
            col += step[k].col;
        }

        return matrix;
    }
}